package in.co.codedoc.ioc;

import java.util.HashSet;
import java.util.Set;

public class ClassUtil
{
	@SuppressWarnings("unchecked")
	public static Set<Class> GetTypes(Class clazz)
	{
		Set<Class> types = new HashSet<Class>();
		Set<Class> interfaces = new HashSet<Class>();
		
		while(!clazz.equals(Object.class))
		{
			types.add(clazz);
			GatherInterfaces(clazz, interfaces);			
			clazz = clazz.getSuperclass();
		}
		
		types.addAll(interfaces);
		
		return types;
	}
	
	@SuppressWarnings("unchecked")
	private static void GatherInterfaces(Class clazz,Set<Class> interfaces)
	{
		Class[] clazzInterfaces = clazz.getInterfaces();
		
		for (int i = 0; i < clazzInterfaces.length; i++)
		{
			if(!interfaces.contains(clazzInterfaces[i]))
			{
				interfaces.add(clazzInterfaces[i]);
				GatherInterfaces(clazzInterfaces[i], interfaces);
			}
		}		
	}
}
